package com.eschool.beans;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import com.eschool.beans.datamodels.CourseDataModel;
import com.eschool.dao.CourseDao;
import com.eschool.model.Course;
import com.eschool.service.SchoolManagementService;
import com.eschool.service.impl.SchoolManagementServiceImpl;
import com.eschool.util.ConstraintsUtil;

@ManagedBean(name="courseStatusUpdateBean")
@ViewScoped
public class CourseStatusUpdateBean{

	private Course[] selectedCoursesList;
    private CourseDataModel courseModel;
	SchoolManagementService service;	
	private static List<Course> courses;	
	private boolean newStatus;

	public CourseStatusUpdateBean() {
		courses = new ArrayList<Course>();
		service = new SchoolManagementServiceImpl();
		
		//System.out.println(UserUtil.currentUser().getName() + "   "  + UserUtil.currentUser().getBranch());
		
		//students = StudentDao.getStudentsByBranch(UserUtil.currentUser().getBranch());

		//students = StudentDao.getStudents(null);
		
		courses = CourseDao.getCourses(ConstraintsUtil.getConstraintsMap());		
		courseModel = new CourseDataModel(courses);
	}

	public boolean isNewStatus() {
		return newStatus;
	}

	public void setNewStatus(boolean newStatus) {
		this.newStatus = newStatus;
	}
	
	public Course[] getSelectedCoursesList() {
		return selectedCoursesList;
	}

	public void setSelectedCoursesList(Course[] selectedCoursesList) {
		this.selectedCoursesList = selectedCoursesList;
	}

	public CourseDataModel getCourseModel() {
		return courseModel;
	}

	public void setCourseModel(CourseDataModel courseModel) {
		this.courseModel = courseModel;
	}

	public static List<Course> getCourses() {
		return courses;
	}

	public static void setCourses(List<Course> courses) {
		CourseStatusUpdateBean.courses = courses;
	}

	public String perform(){
    	
    	List selectedCourses = Arrays.asList(selectedCoursesList);

    	service.updateCoursesStatus(selectedCourses, newStatus);
    	
    	FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
    					FacesMessage.SEVERITY_INFO,"Info Message", "Courses' Status Updated"));
    	
    	return "courseStatusUpdate";
    }
}
